<?php
//http://k.youku.com/player/getFlvPath/sid/343756179428010271e62_00/st/flv/fileid/030002030055ACD4748E582BF367927FD01DF6-06CB-69B2-A2F5-A199E4DCCF2C?K=f2f0730b9e29bfbf261e7745&hd=0&myp=0&ts=372&ymovie=1&ypp=2&ctype=10&ev=1&token=7864&oip=3029060762&ep=STDXjIWIHsMPBSevb3TlGZveYpk9Z2817ST7Cc5hDP5BDWTkEUEhFnRcuB5I94IFk9Ldmd1crBT5Wi4aaRlJ8MRnZ8hILPd2lMljEs31BRSlsfGqfzTRDWgxCvscPiZ6AteJkG8GznA%3D&yxon=1&special=true
if(preg_match("#id\_(\w+)#", $this->url, $matches)) {
    $data = getYouku($matches[1]);
    foreach($data as $k=>$v){
        if($k=="high"||$k=="normal"||$k=="super"||$k=="original") {
            for ($i = 0; $i < count($v); $i++) {
                $retval = _cget($v[$i], '', 1);
                if (preg_match("/location:\s*([^\s]+)/i", $retval, $nn)) {
                    $v[$i] = $nn[1];
                }
                $this->swfstr.= "[flash]" . $v[$i] . "[/flash]\r\n";
            }
        }
    }
}

//优酷算法
function getfileid($fileId,$seed) {
    $mixed = getMixString($seed);
    $ids = explode("*",$fileId);
    unset($ids[count($ids)-1]);
    $realId = "";
    for ($i=0;$i < count($ids);++$i) {
        $idx = $ids[$i];
        $realId .= substr($mixed,$idx,1);
    }
    return $realId;
}

function getMixString($seed) {
    $mixed = "";
    $source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
    $len = strlen($source);
    for($i=0;$i< $len;++$i){
        $seed = ($seed * 211 + 30031) % 65536;
        $index = ($seed / 65536 * strlen($source));
        $c = substr($source,$index,1);
        $mixed .= $c;
        $source = str_replace($c, "",$source);
    }
    return $mixed;
}

function getSid() {
    $sid = time().(rand(0,9000)+10000);
    return $sid;
}

function getkey($key1,$key2){
    $a = hexdec($key1);
    $b = $a ^ 0xA55AA5A5;
    $b = dechex($b);
    return $key2.$b;
}

function yk_d($a)
{
    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    if (!$a) {
        return "";
    }
    $f = strlen($a);
    $b = 0;
    $str = $str;
    for ($c = ""; $b < $f;) {
        $e = charCodeAt($a, $b++) & 255;
        if ($b == $f) {
            $c .= charAt($str, $e >> 2);
            $c .= charAt($str, ($e & 3) << 4);
            $c .= "==";
            break;
        }
        $g = charCodeAt($a, $b++);
        if ($b == $f) {
            $c .= charAt($str, $e >> 2);
            $c .= charAt($str, ($e & 3) << 4 | ($g & 240) >> 4);
            $c .= charAt($str, ($g & 15) << 2);
            $c .= '=';
            break;
        }
        $h = charCodeAt($a, $b++);
        $c .= charAt($str, $e >> 2);
        $c .= charAt($str, ($e & 3) << 4 | ($g & 240) >> 4);
        $c .= charAt($str, ($g & 15) << 2 | ($h & 192) >> 6);
        $c .= charAt($str, $h & 63);
    }
    return $c;
}


function yk_na($a)
{
    $sz = '-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1';
    if (!$a) {
        return "";
    }

    $h = explode(',', $sz);
    $i = strlen($a);
    $f = 0;
    for ($e = ""; $f < $i;) {
        do {
            $c = $h[charCodeAt($a, $f++) & 255];
        } while ($f < $i && -1 == $c);
        if (-1 == $c) {
            break;
        }
        do {
            $b = $h[charCodeAt($a, $f++) & 255];
        } while ($f < $i && -1 == $b);
        if (-1 == $b) {
            break;
        }
        $e .= fromCharCode($c << 2 | ($b & 48) >> 4);
        do {
            $c = charCodeAt($a, $f++) & 255;
            if (61 == $c) {
                return $e;
            }
            $c = $h[$c];
        } while ($f < $i && -1 == $c);
        if (-1 == $c) {
            break;
        }
        $e .= fromCharCode(($b & 15) << 4 | ($c & 60) >> 2);
        do {
            $b = charCodeAt($a, $f++) & 255;
            if (61 == $b) {
                return $e;
            }
            $b = $h[$b];
        } while ($f < $i && -1 == $b);
        if (-1 == $b) {
            break;
        }
        $e .= fromCharCode(($c & 3) << 6 | $b);
    }
    return $e;
}

function yk_e($a, $c)
{
    for ($f = 0, $i = 0, $e = "", $h = 0; 256 > $h; $h++) {
        $b[$h] = $h;
    }
    for ($h = 0; 256 > $h; $h++) {
        $f = (($f + $b[$h]) + charCodeAt($a, $h % strlen($a))) % 256;
        $i = $b[$h];
        $b[$h] = $b[$f];
        $b[$f] = $i;
    }
    for ($q = ($f = ($h = 0)); $q < strlen($c); $q++) {
        $h = ($h + 1) % 256;
        $f = ($f + $b[$h]) % 256;
        $i = $b[$h];
        $b[$h] = $b[$f];
        $b[$f] = $i;
        $e .= fromCharCode(charCodeAt($c, $q) ^ $b[($b[$h] + $b[$f]) % 256]);
    }
    return $e;
}

function fromCharCode($codes)
{
    if (is_scalar($codes)) {
        $codes = func_get_args();
    }
    $str = "";
    foreach ($codes as $code) {
        $str .= chr($code);
    }
    return $str;
}

function charCodeAt($str, $index)
{
    static $charCode = array();
    $key = md5($str);
    $index = $index + 1;
    if (isset($charCode[$key])) {
        return $charCode[$key][$index];
    }
    $charCode[$key] = unpack("C*", $str);
    return $charCode[$key][$index];
}

function charAt($str, $index = 0)
{
    return substr($str, $index, 1);
}

function _cget($url='', $user_agent='',$header=0){
    $USER_AGENT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36";
    if(!$url) return;
    $user_agent = $user_agent ? $user_agent : $USER_AGENT;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, $header);
    if(strlen($user_agent)) curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

    ob_start();
    curl_exec($ch);
    $html = ob_get_contents();
    ob_end_clean();

    if(curl_errno($ch)){
        curl_close($ch);
        return false;
    }
    curl_close($ch);
    if(!is_string($html) || !strlen($html)){
        return false;
    }
    return $html;
    // 判断是否gzip压缩
    if($dehtml = self::_gzdecode($html))
        return $dehtml;
    else
        return $html;
}

function getYouku($vid)
{
//$link = “http://v.youku.com/player/getPlayList/VideoIDS/{$vid}/Pf/4″; //获取视频信息json 有些视频获取不全(土豆网的 火影忍者)
    $blink =  "http://v.youku.com/player/getPlaylist/VideoIDS/". $vid;
    $link = $blink . "/Pf/4/ctype/12/ev/1";
    $retval = _cget($link);
    $bretval = _cget($blink);
    if ($retval) {
        $rs = json_decode($retval, true);
        $brs = json_decode($bretval, true);
        if (!empty($rs['data'][0]['error'])) {
            return false;  //有错误返回false
        }
        $data = array();
        $streamtypes = $rs['data'][0]['streamtypes'];  //可以输出的视频清晰度
        $streamfileids = $rs['data'][0]['streamfileids'];
        $seed = $rs['data'][0]['seed'];
        $segs = $rs['data'][0]['segs'];
        $ip = $rs['data'][0]['ip'];
        $bsegs = $brs['data'][0]['segs'];
        list($sid, $token) = explode('_', yk_e('becaf9be', yk_na($rs['data'][0]['ep'])));
        foreach ($segs as $key => $val) {
            if (in_array($key, $streamtypes)) {
                foreach ($val as $k => $v) {
                    $no = strtoupper(dechex($v['no'])); //转换为16进制 大写
                    if (strlen($no) == 1) {
                        $no = '0' . $no;  //no 为每段视频序号
                    }
//构建视频地址K值
                    $_k = $v['k'];
                    if ((!$_k || $_k == "") || $_k == '-1') {
                        $_k = $bsegs[$key][$k]['k'];
                    }
                    $fileId = getFileid($streamfileids[$key], $seed);
                    $fileId = substr($fileId, 0, 8) . $no . substr($fileId, 10);
                    $ep = urlencode(iconv("gbk", "UTF-8", yk_d(yk_e("bf7e5f01", ((($sid . "_") . $fileId) . "_") . $token))));
//判断后缀类型 、获得后缀
                    $typeArray = array("flv" => "flv", "mp4" => "mp4", "hd2" => "flv", "3gphd" => "mp4", "3gp" => "flv", "hd3" => "flv");
//判断视频清晰度
                    $sharpness = array("flv" => "normal", "flvhd" => "normal", "mp4" => "high", "hd2" => "super", "3gphd" => "high", "3gp" => "normal", "hd3" => "original"); //清晰度 数组
                    $fileType = $typeArray[$key];
                    $data[$sharpness[$key]][$k] = "http://k.youku.com/player/getFlvPath/sid/" . $sid . "_00/st/{$fileType}/fileid/" . $fileId . "?K=" . $_k . "&hd=1&myp=0&ts=" . ((((($v['seconds'] . '&ypp=0&ctype=12&ev=1&token=') . $token) . '&oip=') . $ip) . '&ep=') . $ep;
                }
            }
        }
    }
    $data[title]=$rs['data'][0]['title'];
    $data['img'] = $rs['data'][0]['logo'];
    $data['title'] = $rs['data'][0]['title'];
    $data['url'] = $url;
    return $data;
}


?>